<template>
  <div class="engineerDetails">
    <el-page-header @back="goBack" content="工程详情"></el-page-header>
    <el-card class="box-card">
      <div slot="header" class="clearfix header">
        <span>工程信息</span>
      </div>
      <el-form :model="Engineerform" label-position="left">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程编号"
              ><span>{{ Engineerform.serial_number }}</span></el-form-item
            >
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程名称">
              <span>{{ Engineerform.name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程类型">
              <span>{{ Engineerform.type }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程状态">
              <el-tag
                type="succees"
                v-if="Engineerform.status === '监测中'"
                size="small"
                >监测中</el-tag
              >
              <el-tag
                type="danger"
                v-if="Engineerform.status === '未绑定'"
                size="small"
                >未绑定</el-tag
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户姓名">
              <span>{{ Engineerform.customer_name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户电话">
              <span>{{ Engineerform.customer_telephone }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程地址">
              <span>{{ Engineerform.address }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程主管">
              <span>{{ Engineerform.charge_name }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix header">
        <span>绑定信息</span>
        <el-button
          style="float: right; padding: 3px 0; width: 50px; height: 30px"
          type="success"
          @click="bindClick()"
          >绑定</el-button
        >
        <el-table :data="deviceInfo">
          <el-table-column
            label="序号"
            width="60"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column
            label="设备编号"
            width="120"
            align="center"
            prop="serial_number"
          ></el-table-column>
          <el-table-column
            label="设备名称"
            align="center"
            prop="name"
          ></el-table-column>
          <el-table-column
            label="经度"
            align="center"
            prop="longitude"
          ></el-table-column>
          <el-table-column
            label="纬度"
            align="center"
            prop="latitude"
          ></el-table-column>
          <el-table-column
            label="视频地址"
            align="center"
            prop="video"
          ></el-table-column>
          <el-table-column label="在线状态" width="120" align="center">
            <template v-slot="{ row }">
              <el-tag v-if="row.online_status === 1">在线</el-tag>
              <el-tag v-else type="danger">离线</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" align="center">
            <template v-slot="{ row }">
              <el-button
                type="text"
                v-if="row.online_status == 0"
                @click="onlineClick(row.id, $event)"
                >上线</el-button
              >
              <el-button type="text" v-else @click="onlineClick(row.id, $event)"
                >下线</el-button
              >
              <el-button type="text" @click="unBindClick(row.id)"
                >解绑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="closeDialog"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm" inline>
        <el-form-item
          label="设备"
          :label-width="formLabelWidth"
          prop="device_id"
        >
          <el-select v-model="form.device_id">
            <el-option
              v-for="(item, index) in unBindDevice"
              :label="item.name"
              :value="item.id"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="经度"
          :label-width="formLabelWidth"
          prop="longitude"
        >
          <el-input v-model="form.longitude"></el-input>
        </el-form-item>
        <el-form-item
          label="纬度"
          :label-width="formLabelWidth"
          prop="latitude"
        >
          <el-input v-model="form.latitude"></el-input>
        </el-form-item>
        <el-form-item label="视频流" :label-width="formLabelWidth" prop="video">
          <el-input v-model="form.video"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, postJSON, del } from "@/utils/request.js";

export default {
  name: "Details",
  data() {
    return {
      Engineerform: {},
      deviceInfo: [],
      dialogFormVisible: false,
      form: {},
      title: "",
      rules: {
        device_id: [
          {
            required: true,
            message: "请选择设备",
            trigger: ["blur", "change"],
          },
        ],
        longitude: [
          { required: true, message: "请输入工程纬度", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "请输入工程纬度", trigger: "blur" },
        ],
      },
      formLabelWidth: "120px",
      unBindDevice: [],
    };
  },
  created() {
    this.Engineerform = this.$route.query;
    this.getBindDevice();
    this.getUnBindDevice();
  },
  methods: {
    //事件监听
    goBack() {
      this.$router.back();
    },
    bindClick() {
      this.title = "绑定设备";
      this.dialogFormVisible = true;
    },
    closeDialog() {
      this.$refs.ruleForm.resetFields();
      this.dialogFormVisible = false;
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          get("/engineer/bindService", {
            ...this.form,
            engineer_id: this.Engineerform.id,
          }).then((res) => {
            this.getBindDevice();
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.dialogFormVisible = false;
          });
        } else {
          this.$message({
            message: "表单验证失败",
            type: "error",
          });
        }
      });
    },
    unBindClick(id) {
      this.$confirm("此操作将解绑该工程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        get("/engineer/unbindDevice", {
          device_id: id,
          engineer_id: this.Engineerform.id,
        }).then(() => {
          this.getBindDevice();
          this.getUnBindDevice();
          this.$message({
            type: "success",
            message: "解绑成功!",
          });
        });
      });
    },
    onlineClick(id, event) {
      if (event.srcElement.innerText === "上线") {
        this.$confirm("此操作将开启该设备, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          get("/device/openDevice", { id }).then(() => {
            this.getBindDevice();
            this.getUnBindDevice();
            this.$message({
              type: "success",
              message: "开启成功!",
            });
          });
        });
      } else {
        this.$confirm("此操作将关闭该设备, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          get("/device/closeDevice", { id }).then(() => {
            this.getBindDevice();
            this.getUnBindDevice();
            this.$message({
              type: "success",
              message: "关闭成功!",
            });
          });
        });
      }
    },
    //网络请求
    getBindDevice() {
      get("/device/pageQuery", {
        page: 1,
        pageSize: 100,
        engineer_id: this.Engineerform.id,
      }).then((res) => {
        this.deviceInfo = res.data.list;
      });
    },
    getUnBindDevice() {
      get("/device/pageQuery", {
        page: 1,
        pageSize: 100,
        bind_status: 0,
      }).then((res) => {
        console.log(res);
        this.unBindDevice = res.data.list;
      });
    },
  },
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  margin-top: 15px;
  width: 100%;
}
.header {
  font-size: 18px;
  font-weight: 700;
}
</style>